package com.Bigbluebook.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.Bigbluebook.R;
import com.Bigbluebook.entity.Follow;
import com.Bigbluebook.entity.User;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import com.Bigbluebook.R;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.Bigbluebook.R;
import com.Bigbluebook.entity.User;
import com.squareup.picasso.Picasso;

import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
    private List<User> users;
    private OnItemClickListener onItemClickListener;

    // 接口定义，用于点击事件
    public interface OnItemClickListener {
        void onItemClick(User user);
    }

    // 适配器构造函数
    public MessageAdapter(List<User> users) {
        this.users = users != null ? users : new ArrayList<>();
    }

    // 提供一个方法来设置点击事件监听器
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @NonNull
    @Override
    public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, parent, false);
        return new MessageViewHolder(view, onItemClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
        User user = users.get(position);
        Picasso.get().load(R.drawable.ic_avatar).into(holder.imageViewAvatar);
        holder.textViewName.setText(user.getUsername());

        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(user);
            }
        });
    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    public static class MessageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageViewAvatar;
        TextView textViewName;
        TextView textViewContent;
        TextView textViewTime;

        public MessageViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
            super(itemView);
            imageViewAvatar = itemView.findViewById(R.id.imageViewAvatar);
            textViewName = itemView.findViewById(R.id.textViewName);
            textViewContent = itemView.findViewById(R.id.textViewContent);
            textViewTime = itemView.findViewById(R.id.textViewTime);

            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    User user = (User) itemView.getTag();
                    listener.onItemClick(user);
                }
            });
        }
    }

    public void setUsers(List<User> newUsers) {
        this.users = newUsers;
        notifyDataSetChanged();
    }
}